package com.by_man.presenter.module_health_records.access_records;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.by_man.presenter.R;
import com.by_man.presenter.module_gestureLock.GestureLockActivity;
import com.by_man.repository.UserInfos;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by man on 2017-03-17.
 */
public class OCRReportResultActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocr_report_time);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, OCRActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, OCRActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    protected void onRestart()
    {
        super.onRestart();
        Intent intent=new Intent(this,GestureLockActivity.class);
        intent.putExtra("class",this.getClass().getName());
        intent.putExtra("position", 0);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("OCR结果");
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        String ocr_resulttime = getIntent().getStringExtra("extra_data");
        final ListView lv=(ListView)findViewById(R.id.projecttime);
        final ArrayList<HashMap<String, Object>> listItem = new ArrayList<>();
        try {
            JSONObject temp;
            JSONArray jsonArray = new JSONArray(UserInfos.OCR_Report);
            for (int j = 0; j < jsonArray.length(); j++) {
                temp = (JSONObject) jsonArray.get(j);
                HashMap<String, Object> map = new HashMap<String, Object>();
                final String reportTime = temp.getString("caiji_time");
                if(ocr_resulttime.equals(reportTime))
                {
                    final String reportName = temp.getString("name");
                    final String reportVlue = temp.getString("value");
                    final String unit = temp.getString("unit");
                    final String preference = temp.getString("reference");
                    map.put("reportName", reportName);
                    map.put("reportVlue", reportVlue);
                    map.put("unit", unit);
                    map.put("preference", preference);
                    listItem.add(map);
                }
            }

            SimpleAdapter adapter = new SimpleAdapter(OCRReportResultActivity.this, listItem, R.layout.item_listview_ocr_report_result, new String[]{"reportName","reportVlue","unit","preference"}, new int[]{R.id.ocrkey_value,R.id.ocrvalue,R.id.unit,R.id.preference});
            lv.setAdapter(adapter);
        } catch (JSONException je) {
            je.printStackTrace();
        }
    }
}
